DIY Exercise 7-2: Define and use application properties
Time estimate: 1.5 hours
Objectives
In this exercise, you configure a Mule application to preserve data after it crosses a connection boundary and then define and use application properties for all HTTP Request operation configuration information. You will:
· Preserve data submitted to a flow using a connection boundary.
· Make HTTP requests asynchronously.
· Add property placeholders to a Mule application.
· Create a configuration properties YAML file to store property placeholders in a hierarchy.
· Save common values to global properties.
Scenario
Additional data processing requirements are added for the project. To process the data, you need to make a request to an external endpoint (simulated by making an HTTP Request to another flow in your Mule application). You are also tasked with making the project more maintainable by defining and using application properties for the configuration information for all HTTP Request operations.
Import the starter project
Use your solution from exercise 7-1 or import /files/module07/structure-mod07-solution1.jar (in the MUFundamentals4.x DIY Files zip that you can download from the Course Resources) into Anypoint Studio.
Run the project
Run the project and make a request to http://localhost:8081/flights?destination=SFO with a header named Requester-ID set to "AJ46". Verify that the payload, the destination variable, and the Requester-ID header are accessible in all of the Mule application's flows.
Add an HTTP Listener to the processData flow
Add an HTTP Listener to the processData flow and configure the HTTP Listener to listen for POST requests to /processData.
Replace the processData Flow Reference component with an HTTP Request
Replace the processData Flow Reference component with an HTTP Request configured to make a POST request to http://localhost:8081/processData. Run the project and make the same request.
Answer the following questions
· What is the result of the request to /flights?
· Are the payload, variables, and attributes accessible in all of the flows in the Mule application?
· What data is accessible within the processData flow?
· What data is still accessible in the flow after completing the HTTP Request operation?
Make all data accessible after the external call
Modify the Mule application to preserve the destination, requester-id, and date values. Rename the date key to flight-date-return in the Transform Message component in the processData flow.
Make the HTTP Request's POST occur asynchronously
Move the HTTP Request into an Async scope. Run the project and make the same request.
Answer the following questions
· What is the result of the request to /flights?
· Are the payload, variable, and attributes accessible throughout all flows called in the Mule application?
· What data is preserved within the processData flow?
Add and use property placeholders and configuration properties files
Create a YAML configuration property file with the name env-properties.yaml. Create a ${http.port} property placeholder that is set to 8081 in env-properties.yaml. Add a ${http.host} property placeholder set to "0.0.0.0".
Next, add property placeholders for all configuration information for all HTTP Request operations including host, port, paths, and url.
Once all properties have been created in env-properties.yaml, refactor the Mule application to refer to these property values in env-properties.yaml. There should be no hard-coded values in any of the HTTP operations or HTTP configurations.
Structure the Mule application to increase project maintainability
Move all common connector configurations and the configuration property to a Mule configuration file with the name global.xml.
Add an environment system property name to the configuration file name
Create a configuration property to refer to the configuration properties file ${env}-properties.yaml. In a debug configuration, set the VM argument -Denv=dev, then verify you can run the Mule application and submit requests with a REST client to http://localhost:8081/flights?destination=SFO with a header named Requester-ID set to "AJ46".
Answer the following questions
· What happens if you do not set the -M-Denv=dev parameters when you run the Mule application?
· What happens if you instead try to use -M-Denv=stage when you run the Mule application?
Verify your solution
Load the solutions /files/module07/solutions/structure-mod07-solution2.jar (in the MUFundamentals4.x DIY Files zip that you can download from the Course Resources) and compare your solution.
Answer the following questions
· Within one Mule application, are variables and attributes passed from one flow to another when called via a Flow Reference component?
· Within one Mule application, are variables and attributes transferred from one connection to another (HTTP, JMS, VM)?
· Within one Mule application, do variables get preserved after a response from a connection is returned?
· Within one Mule application, do attributes get preserved after a response from a connection is returned?
· What is another method to transfer variables from one connection to another connection?